<?php
/**
 * Create by PhpStorm
 * User: lijia
 * Data: 2020/8/24
 * Time: 10:27
 */

namespace app\api\controller;

use app\api\business\OrdersBus;
use app\api\validate\Order;
use think\facade\Db;
use think\facade\Filesystem;

class Orders extends UserCommon
{
    public function __construct(){
        parent::__construct();
    }

    /**
     * 创建订单
     * @return \think\response\Json
     */
    public function add(){
        $param = input('param.','','trim');
        Db::startTrans();
        try{
            $orders_id = (new OrdersBus())->addOrder($param);
            Db::commit();
            return resJson(200,'订单创建成功',['orders_id' => $orders_id]);
        }catch (\Exception $e){
            Db::rollback();
            return resJson($e->getCode(),$e->getMessage());
        }
    }


    /**
     * 创建充值订单
     * @return \think\response\Json
     */
    public function recharge(){
        $param = input('param.','','trim');
        if ($param['money'] <= 0) {
            return resJson(0, '充值金额错误', '');
        }
        try{
            $orders_id = (new OrdersBus())->recharge($param);
            return resJson(200,'订单创建成功',['recharge_id' => $orders_id]);
        }catch (\Exception $e){
            return resJson($e->getCode(),$e->getMessage());
        }
    }


    /**
     * 获取订单详情
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getInfo(){
        $id = input('param.id',0,'intval');
        if (!$id) return resJson(0,'订单号错误');
        $data = (new OrdersBus())->info($id);
        return resJson(200,'ok',$data);
    }


    /**
     * 订单列表
     * @return \think\response\Json
     */
    public function getList(){
        $status = input('param.status',0,'intval');
        try{
            $data = (new OrdersBus())->getList($status);
            return resJson(200,'ok',$data);
        }catch (\Exception $e){
            return resJson($e->getCode(),$e->getMessage());
        }
    }


    /**
     * 核销订单
     * @return \think\response\Json
     */
    public function receipt(){
        $id = input('param.id',0,'intval');
        if (!$id) return resJson(0,'参数错误');
        try{
            $res = (new OrdersBus())->receipt($id);
            if ($res){
                return resJson(200,'申请成功');
            }else{
                return resJson(0,'申请失败');
            }
        }catch (\Exception $e){
            return resJson($e->getCode(),$e->getMessage());
        }
    }


    /**
     * 评价订单
     * @return \think\response\Json
     */
    public function comment(){
        $id = input('param.id',0,'intval');
        $comment = input('param.comment','','trim');
        $comment_content = input('param.comment_content','','trim');
        if (!$id || $comment == '') return resJson(0,'参数错误');
        try{
            $res= (new OrdersBus())->comment($id,$comment,$comment_content);
            if ($res){
                return resJson(200,'评价成功');
            }else{
                return resJson(0,'评价失败');
            }
        }catch (\Exception $e){
            return resJson($e->getCode(),$e->getMessage());
        }
    }


    /**
     * 取消订单
     * @return \think\response\Json
     */
    public function cancel(){
        $id = input('param.id',0,'intval');
        if (!$id) return resJson(0,'参数错误');
        try{
            $res= (new OrdersBus())->delete($id);
            if ($res){
                return resJson(200,'取消成功');
            }else{
                return resJson(0,'取消失败');
            }
        }catch (\Exception $e){
            return resJson($e->getCode(),$e->getMessage());
        }
    }

    /**
     * 余额支付商品订单
     */
    public function balancePay()
    {
        $id = input('param.order_id', 0, 'intval');
        try{
            $res= (new OrdersBus())->balancePay($id);
            if ($res){
                return resJson(200,'购买成功');
            }else{
                return resJson(0,'购买失败');
            }
        }catch (\Exception $e){
            return resJson($e->getCode(),$e->getMessage());
        }
    }

}